package xiangmu

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"zhuandan/data_source"
	"zhuandan/models"
)

type FupansRBody struct {
	Data       []models.Fupan `json:"data"`
	PageNo     int            `json:"pageNo"`
	PageSize   int            `json:"pageSize"`
	TotalCount int64          `json:"totalCount"`
	TotalPage  int64          `json:"totalPage"`
}

type FupansBody struct {
	Page     int   `json:"pageNo" from:"pageNo"`
	Pagesize int   `json:"pageSize" from:"pageSize"`
	Otherid  int64 `json:"otherid" gorm:"otherid"`
	Classify int64 `json:"classify" gorm:"classify"` // 1目标 2kr
}

func Fupans(ctx *gin.Context) {
	request := FupansBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	otherid := request.Otherid
	classify := request.Classify
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Fupan
	res := data_source.Db.Where("classify = ?", classify).Where("otherid = ?", otherid)
	res.Model(&models.Fupan{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var fupanidarr []int64
	for _, f := range users {
		fupanidarr = append(fupanidarr, f.ID)
	}
	var admins []models.Fupanadmin
	data_source.Db.Where("fupanid in (?)", fupanidarr).Find(&admins)
	for k, f := range users {
		var fupanadmins []models.Fupanadmin
		for _, a := range admins {
			if f.ID == a.Fupanid {
				fupanadmins = append(fupanadmins, a)
			}
		}
		users[k].Admins = fupanadmins
	}

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FupansRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type FupanBody struct {
	ID           int64  `json:"id" gorm:"id"`
	Otherid      int64  `json:"otherid" gorm:"otherid"`
	Classify     int64  `json:"classify" gorm:"classify"` // 1目标 2kr
	Name         string `json:"name" gorm:"name"`
	Addtime      string `json:"addtime" gorm:"addtime"`
	Adminuserids string `json:"adminuserids" gorm:"adminuserids"`
	Adminid      int64  `json:"adminid" gorm:"adminid"`
}

func Fupanadd(ctx *gin.Context) {
	request := FupanBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	otherid := request.Otherid
	//adminid := request.Adminid
	classify := request.Classify
	addtime := request.Addtime
	adminuserids := request.Adminuserids
	var chengyuanarr []string
	chengyuanidarr := strings.Split(adminuserids, ",")
	var admins []models.Adminuser
	data_source.Db.Where("id in (?)", chengyuanidarr).Find(&admins)
	for _, a := range admins {
		chengyuanarr = append(chengyuanarr, a.Name)
	}
	huiyiadd := models.Fupan{
		Name:     name,
		Otherid:  otherid,
		Classify: classify,
		Addtime:  addtime,
	}
	data_source.Db.Create(&huiyiadd)
	var chengyuans []models.Fupanadmin
	var adminuserarr []string

	for _, v := range admins {
		adminuserarr = append(adminuserarr, v.Name)
		cy := models.Fupanadmin{
			Adminname: v.Name,
			Adminid:   v.ID,
			Fupanid:   huiyiadd.ID,
		}
		chengyuans = append(chengyuans, cy)
	}
	data_source.Db.Create(&chengyuans)

	data_source.Db.Model(&models.Fupan{}).Where("id = ?", huiyiadd.ID).Update("adminusers", strings.Join(adminuserarr, ","))

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Fupanupdate(ctx *gin.Context) {
	request := FupanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	otherid := request.Otherid
	classify := request.Classify
	addtime := request.Addtime
	adminuserids := request.Adminuserids
	var chengyuanarr []string
	chengyuanidarr := strings.Split(adminuserids, ",")
	var admins []models.Adminuser
	data_source.Db.Where("id in (?)", chengyuanidarr).Find(&admins)
	for _, a := range admins {
		chengyuanarr = append(chengyuanarr, a.Name)
	}
	huiyiadd := models.Fupan{
		Name:     name,
		Otherid:  otherid,
		Classify: classify,
		Addtime:  addtime,
	}
	data_source.Db.Where("id = ?", id).Updates(&huiyiadd)
	var chengyuans []models.Fupanadmin
	var adminuserarr []string
	data_source.Db.Where("fupanid = ?", id).Delete(&models.Fupanadmin{})
	for _, v := range admins {
		adminuserarr = append(adminuserarr, v.Name)
		cy := models.Fupanadmin{
			Adminname: v.Name,
			Adminid:   v.ID,
			Fupanid:   id,
		}
		chengyuans = append(chengyuans, cy)
	}
	data_source.Db.Create(&chengyuans)

	data_source.Db.Model(&models.Fupan{}).Where("id = ?", id).Update("adminusers", strings.Join(adminuserarr, ","))

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Fupandel(ctx *gin.Context) {
	request := FupanBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Fupan{})

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
